﻿using FULLTUNNELCONTROLLib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Web;

namespace Tenancy.Helper
{
    /// <summary>
    /// 发送短信接口
    /// </summary>
    public class MsgSend
    {
        public static FullTunnelServiceControl ctr1;
        //企信通用户帐户
        public static string username = "0512C00155278";
        //企信通用户密码
        public static string password = "5278qwer";
        //企信通服务器ip
        public static string ip = "202.102.41.101";
        //企信通服务器端口
        public static string port = "9005";
        //发送类型
        public static string sendtype = "1";
        //企业的id
        public static string comid = "1";
        //是否需要回执 0代表不需要回执，1代表需要回执
        public static string needreport = "1";

        /// <summary>
        /// 发送验证码
        /// </summary>
        /// <returns></returns>
        public static void HB_SendMsg(string HBTel, string Content)
        {
            ctr1 = new FullTunnelServiceControl();
            string strName = username;
            string strPwd = password;
            string strServerip = ip;//218.94.58.243
            string strServerport = port; //9005
            object hr = ctr1.StartAndManageConnect_User(strName, strPwd, strServerip, strServerport);
            if(Int32.Parse(hr.ToString())==0)
            {
                try
                {
                    Thread thread = new Thread(new ThreadStart(delegate { SendFromDbThread(HBTel, Content); }));
                    thread.Start();
                    //object hb = ctr1.CloseConnect();
                }
                catch (Exception ee)
                {
                   
                }
            }
        }

        /// <summary>
        /// 发送短信
        /// </summary>
        /// <param name="HBTel"></param>
        /// <param name="Content"></param>
        private static string SendFromDbThread(string HBTel,string Content)
        {
            string strControl_type = "send";
            string strName = username;    //用户名
            string strPwd = password;     //密码
            string strServerip = ip;
            string strServerport = port;
            string strSendtype = sendtype;
            string strSourcecode = "0";        //必须得填 不需要填发送号码的填0
            string strDestcode = HBTel;        //发送的号码
            string strContent = Content;
            string strComid = comid;            //必须得填 没有的话也必须填
            string strNeedreceipt = needreport;
            string seqID;
            //FullTunnelServiceControl ctr1 = new FullTunnelServiceControl();
            object oOut = null;
            object hr = ctr1.SendBusinessPkg(strControl_type, strName, strPwd, strSourcecode,
                                                                strDestcode, strServerip, strServerport, strContent,
                                                                strSendtype, strComid, strNeedreceipt, out oOut);
            seqID = oOut.ToString();
            return seqID;
            //if (hr.ToString() == "1")
            //{
            //    appendStringCtrl("发送短信成功 SequenceID:" + seqID + "\n");
            //}
            //else if (hr.ToString() == "210")
            //{
            //    appendStringCtrl("发送短信时无连接\n");
            //}
            //else
            //{
            //    //textBox1.Text = "发送业务包失败"+hr.ToString();
            //    //MessageBox.Show("发送业务包fail");
            //    appendStringCtrl("发送短信失败\n");
            //}
        }

       
    }
}